#!/bin/bash
#$Id: _base_script_block.wrap,v 1.1 2012-05-07 13:47:27 remik Exp $
#

# Load usefull functions
if [ ! -f $HOME/scripto/bash/bash_library.sh ]; then
  echo "[error] $HOME/scripto/bash/bash_library.sh not found. Exiting. "
  exit 1
else
  . $HOME/scripto/bash/bash_library.sh
fi

SCRIPTO_DIR=`pwd`

INFO_MODE=DEBUG
#INFO_MODE=INFO


f_standard_input()
{
  msgb "${FUNCNAME[0]} Beginning."
  V_SECTION=$1
  V_SCRIPT=$2

  TMP_DIR_LIST=/tmp/auto_${USER}_DIR_LIST
  if [ -z "$V_SECTION" ]; then
    msgi "No section was provided, please provide one as first parameter"
    msgi "List of available sections:"
    find . -not -path '*/\.*' -type d > $TMP_DIR_LIST
    cat $TMP_DIR_LIST | sed 's/\.\///' | sed 's/\.//' | grep -v '^$'
    exit 1
  fi #if [ -z "$V_SECTION" ]
  if [ ! -d "$SCRIPTO_DIR/$V_SECTION" ]; then
    msgi "Provided section(directory) does not exists."
    msgi "List of available sections:"
    find . -not -path '*/\.*' -type d > $TMP_DIR_LIST
    cat $TMP_DIR_LIST | sed 's/\.\///' | sed 's/\.//' | grep -v '^$'

    exit 1
  fi #if [ ! -d "$V_SECTION" ]


  TMP_SCRIPT_LIST=/tmp/auto_${USER}_SCRIPT_LIST
  if [ -z "$V_SCRIPT" ]; then
    msgi "No script was provided, please provide one as second parameter"
    msgi "List of available scripts in section $V_SECTION:"
    find ./$V_SECTION -not -path '*/\.*' -type f > $TMP_SCRIPT_LIST
    cat $TMP_SCRIPT_LIST | sed "s/\/$V_SECTION\///" | sed 's/\.//'
    exit 1
  fi #if [ -z "$V_SCRIPT" ]
  if [ ! -f "$V_SECTION/$V_SCRIPT" ]; then
    msgi "Provided script name does not exists" 
    msgi "List of available scripts in section $V_SECTION:"
    find ./$V_SECTION -not -path '*/\.*' -type f > $TMP_SCRIPT_LIST
    cat $TMP_SCRIPT_LIST | sed "s/\/$V_SECTION\///" | sed 's/\.//'
    exit 1
  fi #if [ -f "$V_SECTION/$V_SCRIPT" ]
  msgb "${FUNCNAME[0]} Finished."
} #f_standard_input

# I want this
# http://xmodulo.com/create-dialog-boxes-interactive-shell-script.html
# using whiptail

f_whiptail_menu_section()
{
  msgb "${FUNCNAME[0]} Beginning."
  run_command_e "cd $SCRIPTO_DIR"

  msgd "Par: $1"
  if [ -z "$1" ]; then
    msgd "No default option provided, starting with 1."
    V_DEFAULT_OPTION=1
  else
    V_DEFAULT_OPTION=$1
  fi
 
  msgd "V_DEFAULT_OPTION: $V_DEFAULT_OPTION"

  TMP_DIR_LIST=/tmp/auto_${USER}_DIR_LIST
  msgd "List of available sections:"
  find . -not -path '*/\.*' -type d > $TMP_DIR_LIST
  V_SECTION_MENU=`cat $TMP_DIR_LIST | sort | sed 's/\.\///' | sed 's/\.//' | grep -v '^$' | nl -b a`

  OPTION=$(whiptail --title "Test Menu Dialog" --menu "Choose your option" 15 60 4 $V_SECTION_MENU --default-item $V_DEFAULT_OPTION 3>&1 1>&2 2>&3)
  V_MENU_EXIT_STATUS=$?
  msgd "V_MENU_EXIT_STATUS: $V_MENU_EXIT_STATUS"
 
  if [ $V_MENU_EXIT_STATUS -eq 0 ]; then 

    msgd "OPTION: $OPTION"
    V_SELECTED_SECTION=`cat $TMP_DIR_LIST | sort | sed 's/\.\///' | sed 's/\.//' | grep -v '^$' | nl -b a | grep $OPTION`
    msgd "V_SELECTED_SECTION: $V_SELECTED_SECTION"

    D_SELECTED_SECTION=`echo $V_SELECTED_SECTION | awk '{print $2}'`
    msgd "D_SELECTED_SECTION: $D_SELECTED_SECTION"

    run_command_e "cd $SCRIPTO_DIR/$D_SELECTED_SECTION"

    f_whiptail_menu_script
    
    f_whiptail_menu_section $OPTION
  else
    msgd "Cancel"
  fi


  msgb "${FUNCNAME[0]} Finished."
} #f_whiptail_menu_section


f_whiptail_menu_script()
{
  msgb "${FUNCNAME[0]} Beginning."
  #run_command_d "pwd"
  msgd "Par: $1"
  if [ -z "$1" ]; then
    msgd "No default option provided, starting with 1."
    V_DEFAULT_OPTION=1
  else
    V_DEFAULT_OPTION=$1
  fi

  TMP_SCRIPT_LIST=/tmp/auto_${USER}_SCRIPT_LIST
  find ./$V_SECTION -not -path '*/\.*' -type f -o -type l > $TMP_SCRIPT_LIST

  V_SECTION_MENU=`cat $TMP_SCRIPT_LIST | sort | sed "s/\/$V_SECTION\///" | sed 's/\.//' | nl -b a`

  OPTION=$(whiptail --title "Scripts Menu Dialog" --menu "Choose your option" 30 80 20 $V_SECTION_MENU --default-item $V_DEFAULT_OPTION 3>&1 1>&2 2>&3)
  V_MENU_EXIT_STATUS=$?
  msgd "V_MENU_EXIT_STATUS: $V_MENU_EXIT_STATUS"
 
  if [ $V_MENU_EXIT_STATUS -eq 0 ]; then 

    msgd "OPTION: $OPTION"
    V_SELECTED_SECTION=`cat $TMP_SCRIPT_LIST | sort | sed "s/\/$V_SECTION\///" | sed 's/\.//' | nl -b a | tr -s '[:blank:]' | grep "^ $OPTION"`
    msgd "V_SELECTED_SECTION: $V_SELECTED_SECTION"


    D_SELECTED_SECTION=`echo $V_SELECTED_SECTION | awk '{print $2}'`
    msgd "D_SELECTED_SECTION: $D_SELECTED_SECTION"

    #now run the command
    clear
    echo | tee -a ${LOG_NAME}_raw
    #run_command "pwd"
    # Create a section name from the name of the script executed
    V_SECTION_NAME=`echo $D_SELECTED_SECTION | sed 's/\///' | sed 's/_/\ /g' | sed 's/\.sh$//' | awk '{print "*** "$0}'`
    msgd "V_SECTION_NAME: $V_SECTION_NAME"
    echo "$V_SECTION_NAME" | tee -a ${LOG_NAME}_raw

    msgd "LOG_NAME: $LOG_NAME"
    run_command ".$D_SELECTED_SECTION" | tee -a ${LOG_NAME}_raw
 
    msgw "Press any key to continue." 
    read
    # return to scripts   

    f_whiptail_menu_script $OPTION
    
  else
    msgd "Cancel. We should go up to the main menu now."
  fi

  msgb "${FUNCNAME[0]} Finished."
} #f_whiptail_menu_script


f_human_readable_log()
{
  msgb "${FUNCNAME[0]} Beginning."
  F_LOG=$1
  check_file $F_LOG
  cat $F_LOG | grep -v '\[block\]' | grep -v '\[wait\]' | grep -v '\[debug\]' | sed 's/^|\ ........\ ..\:..\:..\ //' | sed 's/^"//' | sed 's/"$//' > ${F_LOG}_hr
 
  # remove clear sign
  cat -v ${F_LOG}_hr | sed 's/\^\[\[H\^\[\[2J//' > ${F_LOG}_hr_tmp
  mv ${F_LOG}_hr_tmp ${F_LOG}_hr

  # remove [info] with color
  cat -v ${F_LOG}_hr | sed 's/\^\[\[32m\[info\]\ \ \ \ \ \^\[\[39m\^\[\[49m//' > ${F_LOG}_hr_tmp
  mv ${F_LOG}_hr_tmp ${F_LOG}_hr

  # remove control pwd in auto dir
  cat -v ${F_LOG}_hr | grep -v "$SCRIPTO_DIR" > ${F_LOG}_hr_tmp
  mv ${F_LOG}_hr_tmp ${F_LOG}_hr

  # remove script execution
  cat -v ${F_LOG}_hr | grep -v "^\.\/" > ${F_LOG}_hr_tmp
  mv ${F_LOG}_hr_tmp ${F_LOG}_hr
    
  # remove color
  cat -v ${F_LOG}_hr | sed 's/\^\[\[32m//' > ${F_LOG}_hr_tmp
  mv ${F_LOG}_hr_tmp ${F_LOG}_hr

  # remove action tag
  cat -v ${F_LOG}_hr | sed 's/\^\[\[34m\[action\]\ \^\[\[39m\^\[\[49m//' > ${F_LOG}_hr_tmp
  mv ${F_LOG}_hr_tmp ${F_LOG}_hr

  # change 'Executing SQL:' into 'SQL>'
  cat -v ${F_LOG}_hr | sed 's/Executing\ SQL\:/SQL\>/' > ${F_LOG}_hr_tmp
  mv ${F_LOG}_hr_tmp ${F_LOG}_hr
   

  msgb "${FUNCNAME[0]} Finished."
} #f_human_readable_log


# Main section
#f_standard_input $1 $2

f_whiptail_menu_section
f_human_readable_log "${LOG_NAME}_raw"

